package no.ntnu.fp.gui.guiElements;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.sql.Date;
import java.sql.Time;

import no.ntnu.fp.model.Appointment;
import no.ntnu.fp.model.CalendarHelper;
import no.ntnu.fp.model.Person;

public class AppointmentBox {

	Appointment myAppointment;

	public Appointment getMyAppointment() {
		return myAppointment;
	}

	public void setMyAppointment(Appointment myAppointment) {
		this.myAppointment = myAppointment;
	}

	static final Color lightBlue = new Color(100, 100, 255, 230);
	static final Color lightGreen = new Color(50, 200, 50, 230);
	static final Color lightBlueInactive = new Color(100, 100, 255, 100);
	static final Color lightGreenInactive = new Color(50, 200, 50, 100);

	int posX, posY, height, width;

	public int getPosX() {
		return posX;
	}

	public void setPosX(int posX) {
		this.posX = posX;
	}

	public int getPosY() {
		return posY;
	}

	public void setPosY(int posY) {
		this.posY = posY;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public AppointmentBox(Appointment appointment) {
		myAppointment = appointment;

	}

	public void draw(Graphics2D g2d, int x, int y, int width, int height,
			CalendarHelper calHelp) {

		if (myAppointment.getAttending().contains(Person.getCurrentUser())) {
			switch (myAppointment.getType()) {
			case PERSONAL:
				g2d.setColor(lightGreen);

				break;
			case WORK:
				g2d.setColor(lightBlue);

				break;
			default:
				g2d.setColor(Color.RED);
				break;
			}
		} else {
			switch (myAppointment.getType()) {
			case PERSONAL:
				g2d.setColor(lightGreenInactive);

				break;
			case WORK:
				g2d.setColor(lightBlueInactive);

				break;
			default:
				g2d.setColor(Color.RED);
				break;
			}
		}

		x = getXposFromDate(myAppointment.getStartDate(), x, calHelp);
		height = getYposFromDate(myAppointment.getEnd(), y)
				- getYposFromDate(myAppointment.getStart(), y);

		y = getYposFromDate(myAppointment.getStart(), y);

		posX = x;
		posY = y;
		this.height = height;
		this.width = width;

		g2d.fillRoundRect(x + 1, y + 1, width - 7, height - 2, 10, 10);
		if (myAppointment.getOwner().id == Person.getCurrentUser().id) {
			// Current user ER sjef for denne avtalen
			g2d.setColor(Color.BLACK);
		} else {
			// Current user er IKKE sjef for denne avtalen
			g2d.setColor(Color.BLUE);
		}

		g2d.drawRoundRect(x + 1, y + 1, width - 7, height - 2, 10, 10);
		g2d.setColor(Color.BLACK);

		if (height <= 20 || width <= 20) {
			g2d.drawString("...", x + width / 2 - 5, y + height / 2 + 3);
			return;
		}

		if (height > 20 && width > myAppointment.getTitle().length() * 4) {

			if (myAppointment.getTitle().length() > (int) (Math.floor(width
					/ (g2d.getFont().getSize() - 3)))) {
				if ((int) (Math.floor(width / (g2d.getFont().getSize() - 3))) - 1 > 0) {
					g2d.drawString(
							myAppointment
									.getTitle()
									.substring(
											0,
											(int) (Math
													.floor(width
															/ (g2d.getFont()
																	.getSize() - 3))) - 1)
									+ "...", x + 5, y + 20);

				}

			} else {
				g2d.drawString(myAppointment.getTitle(), x + 5, y + 20);
			}

			// g2d.drawString(myAppointment.getTitle(), x + 10, y + 20);
		} else {
			g2d.drawString("...", x + 10, y + 20);
		}
		/*
		 * if (height > 20&&width>20) { if
		 * (myAppointment.getDescription().length() >
		 * (int)(Math.floor(width/(g2d.getFont().getSize())))) {
		 * g2d.drawString(myAppointment.getDescription().substring(0,
		 * (int)(Math.floor(width/(g2d.getFont().getSize()-5))))+"...", x + 5, y
		 * + 40); } else { g2d.drawString(myAppointment.getDescription(), x +
		 * 10, y + e40); } }
		 */

		String startTid = myAppointment.getStart().toString(), slutt = myAppointment
				.getStart().toString();

		if (height > (y + 60) && width > startTid.substring(0, 5).length() * 8) {
			g2d.drawString(startTid.substring(0, 5),
					x + getXCentered(startTid, g2d), y + 40);
			g2d.drawString(slutt.substring(0, 5), x + getXCentered(slutt, g2d),
					y + 60);
			g2d.drawString("-", x + getXCentered("-", g2d), y + 50);
		} else {
			// g2d.drawString("...", x+5, y+40);
			/*
			 * Write nothings
			 */
		}
		if (height > (y + 80)
				&& myAppointment.getOwner().id == Person.getCurrentUser().id) {
			g2d.drawString("(*)", x + getXCentered("(*)", g2d), y + 80);
		}

	}

	public int getXCentered(String a, Graphics2D g2d) {

		return (int) ((width / 2d) - (a.length() * 2));

	}

	public void setAppointment(Appointment ap) {
		myAppointment = ap;
	}

	public int getXposFromDate(Date theDate, int x, CalendarHelper calHelp) {

		return ((CalendarPanel2.getDay(theDate)
				- CalendarPanel2.getDay(calHelp.getFirstCalendarDayDate()) + 7) % 7)
				* x + 32;
	}

	public int getYposFromDate(Time theDate, int y) {
		if (theDate != null) {
			if (theDate.getMinutes() != 0) {
				return (int) ((theDate.getHours() - 5) * y)
						+ (int) ((y + 1) * ((double) (theDate.getMinutes()) / 60d));
				// System.out.println(min);
			}
			return (int) ((theDate.getHours() - 5) * y);

		}

		return -1;
	}

	public boolean isPointInside(Point p) {

		if (p.getX() >= posX && p.getX() <= (posX + width)) {
			if (p.getY() >= posY && p.getY() <= (posY + height)) {
				return true;
			}

		}
		return false;
	}
}
